package com.quyunshuo.module.home.activity.main

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.util.Log
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.Toast
import androidx.activity.viewModels
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.viewpager2.widget.ViewPager2
import com.quyunshuo.edwingoal.base.BaseApplication
import com.quyunshuo.edwingoal.base.utils.BarUtils.addMarginTopEqualStatusBarHeight
import com.quyunshuo.edwingoal.common.ui.BaseActivity
import com.quyunshuo.module.home.adapter.ViewPagerAdapter
import com.quyunshuo.module.home.databinding.HomeActivityMainBinding
import com.quyunshuo.module.home.util.AppExit
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : BaseActivity<HomeActivityMainBinding, SharedViewModel>() {
    companion object {
        fun newInstance(context: Context) {
            context.startActivity(Intent(context, MainActivity::class.java))
        }
    }
    private val REQUEST_READ_STORAGE = 101

    /**
     * 通过 viewModels() + Hilt 获取 ViewModel 实例
     */
    override val mViewModel by viewModels<SharedViewModel>()


    override fun createVB() = HomeActivityMainBinding.inflate(layoutInflater)



    override fun HomeActivityMainBinding.initView() {
        addMarginTopEqualStatusBarHeight(viewPager)
        // 创建适配器并设置
        val adapter = ViewPagerAdapter(this@MainActivity)
        viewPager.adapter = adapter

        // 监听页面变化更新按钮状态
        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                when (position) {
                    0 -> { toggleTabs(home)}
                    1->{toggleTabs(find)}
                    2->{toggleTabs(me)}
                    else -> {toggleTabs(setting)}
                }
            }
        })

        if (PermissionUtils.hasStepCounterPermission(this@MainActivity)) {
            startStepCounting()
        } else {
            PermissionUtils.requestStepCounterPermission(this@MainActivity)
        }
        checkPermissions()


    }

    private fun startStepCounting() {
        mViewModel.init()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            PermissionUtils.REQUEST_ACTIVITY_RECOGNITION_PERMISSION,
            PermissionUtils.REQUEST_BODY_SENSORS_PERMISSION -> {
                if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    startStepCounting()
                } else {
                    Toast.makeText(this, "需要计步权限才能使用此功能", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    private fun checkPermissions() {
        if (PermissionUtils.hasStepCounterPermission(this)) {
            startStepCounting()
        }
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        Log.d("wenlong", "onNewIntent: ")
    }



    override fun HomeActivityMainBinding.initListener() {
        home.setOnClickListener {
            viewPager.currentItem = 0
            toggleTabs(home) }
        find.setOnClickListener {
            viewPager.currentItem = 1
            toggleTabs(find) }
        me.setOnClickListener {
            viewPager.currentItem = 2
            toggleTabs(me) }
        setting.setOnClickListener {
            viewPager.currentItem = 3
            toggleTabs(setting) }
    }

    private fun toggleTabs(home: LinearLayout) {
        // 1行重置所有状态
        unSelTabs(listOf(mBinding.home,mBinding.find,mBinding.me,mBinding.setting))

        // 1行设置选中状态
        (home as? ViewGroup)?.getChildAt(0)?.isSelected = true  // 图标
        (home as? ViewGroup)?.getChildAt(1)?.isSelected = true  // 文本
    }

    private fun unSelTabs(listOf: List<LinearLayout>) {
        listOf.forEach {
            (it as? ViewGroup)?.getChildAt(0)?.isSelected = false  // 图标
            (it as? ViewGroup)?.getChildAt(1)?.isSelected = false  // 文本
        }

    }

    override fun initObserve() {


    }


    override fun initRequestData() {

    }

    override fun onBackPressed() {
        AppExit.onBackPressed(this)
    }


}